package com.windy.dailynews.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.windy.dailynews.R;
import com.windy.dailynews.model.NewsDetail;
import com.windy.dailynews.presenter.NewsDetailContract;
import com.windy.dailynews.presenter.impl.NewsDetailPresenter;
import com.windy.dailynews.ui.view.WebViewBase;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by dingfeng on 2016/11/21.
 */
public class NewsDetailActivity extends BaseActivity implements NewsDetailContract.View {
    private String newsCover;
    private String title;
    private int newsId;
    private NewsDetailContract.Presenter mPresenter;

    @Bind(R.id.appbar)
    AppBarLayout mAppbarLayout;
    @Bind(R.id.collapsinglayout)
    CollapsingToolbarLayout mCollapsingToolbarLayout;
    @Bind(R.id.imageview)
    ImageView mImageView;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.webview)
    WebViewBase mWebView;
    @Bind(R.id.loading)
    View loading;
    @Bind(R.id.load_failed)
    View load_failed;

    @OnClick(R.id.bt_retry)
    void retry() {
        mPresenter.loadData(newsId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        ButterKnife.bind(this);
        init();
        handleIntent(getIntent());
    }

    private void init() {
        mAppbarLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        mCollapsingToolbarLayout.setTitle(" ");
        mCollapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.blue));
    }

    private void handleIntent(Intent intent) {
        if (intent == null) {
            finish();
        }
        newsId = intent.getIntExtra("newsId", -1);
        mWebView.setView(this);
        mPresenter = new NewsDetailPresenter(this);
        mPresenter.loadData(newsId);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showNewsContent(NewsDetail detail) {
        newsCover = detail.getImage();
        title = detail.getTitle();
        String css = "<link rel=\"stylesheet\" href=\"file:///android_asset/css/news.css\" type=\"text/css\">";
        String html = "<html><head>" + css + "</head><body>" + detail.getBody() + "</body></html>";
        html = html.replace("<div class=\"img-place-holder\">", "");
        mWebView.loadDataWithBaseURL("x-data://base", html, "text/html", "UTF-8", null);
    }

    @Override
    public void setLoading() {
        loading.setVisibility(View.VISIBLE);
        load_failed.setVisibility(View.GONE);
    }

    @Override
    public void setError() {
        loading.setVisibility(View.GONE);
        load_failed.setVisibility(View.VISIBLE);
    }

    @Override
    public void setPresenter(NewsDetailContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void loadFinish() {
        Glide.with(getApplicationContext())
                .load(newsCover)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mImageView);
        mCollapsingToolbarLayout.setTitle(title);
        mWebView.setVisibility(View.VISIBLE);
        loading.setVisibility(View.GONE);
        load_failed.setVisibility(View.GONE);
    }

}
